package com.wanhai.itm.wditm145.lov;


import com.wanhai.vo.itm.ITM3012VO;
import com.wanhai.zkbase.dao.IBaseDAO;
import com.wanhai.zkbase.dao.JndiDAOImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zkoss.bind.Binder;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.GroupsModel;
import org.zkoss.zul.ListitemRenderer;

/**
 *
 * @author Richard Shih
 */
public class WDITM145_ITEMNO_VM {

    private Log logger = LogFactory.getLog(getClass());
    private GroupsModel model = null;
    private ListitemRenderer renderer = null;
    String callbackCmd = null;
    ITM3012VO selectedItem = null;
    //service
//    DefaultDataService lovDataService = new DefaultDataService<ITM3012VO>();
    //DAO
    IBaseDAO daoImpl=new JndiDAOImpl("java:OraLovDS"); 

    @Init
    public void init(@ExecutionArgParam("callbackCmd") String callbackCmd,
            @ExecutionArgParam("params") Map<String, Object> params) {
        this.callbackCmd = callbackCmd;

        try {
            ITM3012VO cond = new ITM3012VO();
            cond.setITEMNO("%");
            List<ITM3012VO> list = daoImpl.query(cond);
            ITM3012VO[] rtnValues = new ITM3012VO[list.size()];
            list.toArray(rtnValues);
            model = new PlanitemGroupsModel(rtnValues);
            renderer = new PlanitemListRenderer();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    public ListitemRenderer getRenderer() {
        return renderer;
    }

    public void setRenderer(ListitemRenderer renderer) {
        this.renderer = renderer;
    }

    public ITM3012VO getSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(ITM3012VO selectedItem) {
        this.selectedItem = selectedItem;
    }

    public GroupsModel getModel() {
        return model;
    }

    public void setModel(GroupsModel model) {
        this.model = model;
    }

    @Command("choose")
    public void onLOVConfirm(@ContextParam(ContextType.VIEW) Component comp) {
//        Binder binder = (Binder) comp.getParent().getAttribute("binder");
//        if (binder == null) {
//            return;
//        }
//
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("callbackValue", this.selectedItem);
//
//        binder.postCommand(this.callbackCmd, params);
        comp.detach();
    }
    
    @Command("cancel")
    public void onLOVCancel(@ContextParam(ContextType.VIEW) Component comp){
        comp.detach();
    }
}
